<template>
  <div class="conten">
    <el-form ref="form" :rules="rules" :model="formData" label-width="80px">
      <el-form-item label="图片" prop="image_url">
        <Upload
          :url="formData.image_url"
          tip="建议图片比例 9:16，图片大小不能超过2MB，分辨率不能
          超过1080*1080，支持jpg、jpeg、png格式"
          @resultUpload="onUpLoad"
        ></Upload>
      </el-form-item>
      <el-form-item label="跳转短剧" prop="playlet_id">
        <el-select v-model="formData.playlet_id" clearable placeholder="请选择" style="width:250px;">
          <el-option v-for="item in optionsList.playletList" :key="item.value" :label="item.label" :value="item.value" :disabled="item.status * 1 === 2"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="btns" :loading="loading" @click="onSubmit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { commonData } from '@/api/common'
import Upload from '@/common/component/upload.vue'
export default {
  name: 'WallpaperAdd',
  components: {
    Upload
  },
  props: {
    data: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      loading: false,
      formData: {
        image_url: '',
        playlet_id: '',
        mini_app_id: window.localStorage.getItem('mini_app_id')
      },
      optionsList: {
        playletList: []
      },
      rules: {
        playlet_id: [{ required: true, message: '请选择短剧', trigger: 'blur' }],
        image_url: [{ required: true, message: '请选择图片', trigger: 'blur' }]
      }
    }
  },
  async mounted() {
    console.log(this.data)
    await this.onGetPlayletList()
    if (this.data.id) {
      this.formData = this.data
      this.formData.playlet_id = this.data.playlet_id * 1
      console.log(this.data)
    }
  },
  methods: {
    onSubmit() {
      this.loading = true

      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit('ok', {
            type: 'Add',
            data: this.formData
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
      this.loading = false
    },
    onUpLoad(url, type) {
      this.formData.image_url = url
    },
    async onGetPlayletList() {
      const res = await commonData({
        type: 'miniAppPlaylet',
        mini_app_id: this.formData.mini_app_id
      })
      if (res.status_code == 1) {
        this.$set(this.optionsList, 'playletList', res.data.data)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.conten {
  width: 400px;
  padding:20px;
  .btns{
  }
}
</style>